package com.jcl.recruit.basic.register.impl;

import com.jcl.recruit.basic.register.RecruitRegister;
import com.jcl.recruit.basic.register.RegisterCallback;
import com.jcl.recruit.basic.register.RegisterUserCallback;
import com.jcl.recruit.basic.register.model.RegisterCorpResult;
import com.jcl.recruit.basic.register.model.RegisterEnum;
import com.jcl.recruit.basic.register.model.RegisterResult;
import com.jcl.recruit.basic.register.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @ClassName RecruitRegisterImpl

 * @Author yyliu
 * @Date 2018/12/17 10:17
 * @Version 1.0
 **/
@Component("recruitRegister")
public class RecruitRegisterImpl implements RecruitRegister {

    @Autowired
    private ConsoleRecruitRegister consoleRecruitRegister;

    @Autowired
    private CorpRecruitRegister corpRecruitRegister;

    @Autowired
    private UserRecruitRegister userRecruitRegister;

    @Autowired
    @Qualifier("registerCallback")
    private RegisterCallbackImpl registerCallback;

    @Autowired
    @Qualifier("registerUserCallback")
    private RegisterUserCallbackImpl registerUserCallback;

    @Override
    public RegisterResult register(RegisterVo registerVo, RegisterEnum registerEnum, RegisterCallback callback) {
        String corpCode = consoleRecruitRegister.getCorpCode();
        RegisterResult result = this.registerConsoleCorp(corpCode, registerVo, registerEnum);
        RegisterCorpResult corpResult = this.registerCorp(corpCode, registerVo);
        if (null != result && null != corpResult) {
            result.setCorpCode(corpCode);
            result.setUserId(corpResult.getUserId());

            if (null != callback) {
                registerCallback.refreshConsoleCorpInfo(result.getConsoleCorpInfo(), corpResult.getUserId());
                callback.registerCorp(registerEnum, registerVo, result);
            } else {
                registerCallback.registerCorp(registerEnum, registerVo, result);
            }
        }
        return result;
    }

    @Override
    public RegisterResult addRegisterUser(String corpCode,
                                          RegisterVo registerVo,
                                          RegisterEnum registerEnum,
                                          RegisterUserCallback callback) {
        RegisterResult result = this.registerUser(corpCode, registerVo, registerEnum);
        if (null != result) {
            if (null != callback) {
                callback.registerUser(registerEnum, registerVo, result);
            } else {
                registerUserCallback.registerUser(registerEnum, registerVo, result);
            }
        }
        return result;
    }

    public RegisterCorpResult registerCorp(String corpCode, RegisterVo registerVo) {
        corpRecruitRegister.setRegisterVo(registerVo);
        return corpRecruitRegister.register(corpCode, registerVo.getPassword());
    }

    public RegisterResult registerConsoleCorp(String corpCode,
                                              RegisterVo registerVo,
                                              RegisterEnum registerEnum) {
        consoleRecruitRegister.setRegisterVo(registerVo);

        return consoleRecruitRegister.register(corpCode, registerEnum);
    }

    public RegisterResult registerUser(String corpCode,
                                       RegisterVo registerVo,
                                       RegisterEnum registerEnum) {
        userRecruitRegister.setRegisterVo(registerVo);
        return userRecruitRegister.register(corpCode, registerVo.getPassword(), registerEnum);
    }
}
